package fun.qu_an.minecraft.mini_games_helper.client.handle;

import fun.qu_an.minecraft.mini_games_helper.client.config.MiniGamesHelperClientConfig;
import me.jellysquid.mods.sodium.client.gui.options.Option;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.api.v0.IrisApi;
import net.irisshaders.iris.gui.option.IrisVideoSettings;
import net.irisshaders.iris.pathways.colorspace.ColorSpace;
import net.minecraft.client.MinecraftClient;
import net.minecraft.world.GameMode;

import java.util.Optional;

@Environment(EnvType.CLIENT)
public class ForceOptionHelperSodium {
	//	private static final Map<SimpleOption<?>, SimpleOption<?>> creative = new IdentityHashMap<>();
//	private static final Map<SimpleOption<?>, SimpleOption<?>> survival = new IdentityHashMap<>();
//	private static final Map<SimpleOption<?>, SimpleOption<?>> adventure = new IdentityHashMap<>();
//	private static final Map<SimpleOption<?>, SimpleOption<?>> spectator = new IdentityHashMap<>();
//	private static Map<SimpleOption<?>, SimpleOption<?>> current = Map.of();
//	private static final Map<String, Pair<SimpleOption<?>, Class<?>>> vanillaOptions = new HashMap<>();
	private static ColorSpace oldCSValue = null;
	private static String shaderPackNameOld;
	private static Boolean areShadersEnabledOld;

	public static Option<?> get(String key) {
		return null;
	}

	public static Optional<ColorSpace> getColorSpace(GameMode gameMode) {
		return MiniGamesHelperClientConfig.get().getColorSpace(gameMode).map(cs
			-> switch (cs) {
			case SRGB -> ColorSpace.SRGB;
			case DCI_P3 -> ColorSpace.DCI_P3;
			case DISPLAY_P3 -> ColorSpace.DISPLAY_P3;
			case REC2020 -> ColorSpace.REC2020;
			case ADOBE_RGB -> ColorSpace.ADOBE_RGB;
		});
	}

	public static boolean shouldTryServerRP(GameMode gameMode) {
		return MiniGamesHelperClientConfig.get().shouldTryServerRP(gameMode);
	}

	public static boolean isHidePlayerList(GameMode gameMode) {
		return MiniGamesHelperClientConfig.get().isHidePlayerList(gameMode);
	}

	public static void applyColorSpace(GameMode gameMode) {
		getColorSpace(gameMode).ifPresentOrElse(cs -> {
			if (oldCSValue == null) {
				oldCSValue = IrisVideoSettings.colorSpace;
			}
			IrisVideoSettings.colorSpace = cs;
		}, ForceOptionHelperSodium::revokeColorSpace);
	}

	public static void applyServerRPShader(GameMode gameMode) {
		if (!shouldTryServerRP(gameMode)) {
			revokeShader();
			return;
		}

		if (areShadersEnabledOld == null) {
			areShadersEnabledOld = Iris.getIrisConfig().areShadersEnabled();
		}
		if (shaderPackNameOld == null) {
			shaderPackNameOld = Iris.getIrisConfig().getShaderPackName().orElse("");
		}
//		MinecraftClient.getInstance().execute(() -> {
		IrisApi api = IrisApi.getInstance();
		api.getConfig().setShadersEnabledAndApply(true);
//		});
	}

	public static void apply(GameMode gameMode) {
		applyColorSpace(gameMode);
		applyServerRPShader(gameMode);
	}

	public static void revoke() {
		revokeColorSpace();
		revokeShader();
	}

	private static void revokeColorSpace() {
		if (oldCSValue != null) {
			IrisVideoSettings.colorSpace = oldCSValue;
			oldCSValue = null;
		}
	}

	private static void revokeShader() {
		if (shaderPackNameOld == null && areShadersEnabledOld == null) {
			return;
		}

		if (shaderPackNameOld != null) {
			Iris.getIrisConfig().setShaderPackName(shaderPackNameOld);
			System.out.println(shaderPackNameOld);
			shaderPackNameOld = null;
		}

		if (areShadersEnabledOld != null) {
			IrisApi api = IrisApi.getInstance();
			api.getConfig().setShadersEnabledAndApply(areShadersEnabledOld);
			System.out.println(areShadersEnabledOld);
			areShadersEnabledOld = null; // ummm
		}
	}
}
